/*
Copyright 2000-2011 Francois de Bertrand de Beuvron

This file is part of CoursBeuvron.

CoursBeuvron is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CoursBeuvron is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CoursBeuvron.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.insa.glaizot;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Ordres de base permettant de se connecter à un SGBD
 * 
 * @author Francois de Beuvron (INSA Strasbourg)
 */
public class TestSGBD {

    public static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    public static Connection defautCon = etabliConnection();

    public static Connection etabliConnection() {
        try {
            Connection con;

            Class.forName(TestSGBD.MYSQL_DRIVER);

            // maintenant que j'ai tous ces renseignements, je peux effectivement me
            // connecter à la base de donnée
            con = DriverManager.getConnection("jdbc:mysql://crpilamp.insa-strasbourg.fr:3306/4glaitan", "4glaitan", "gUDaw963");

            con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

            return con;
        } catch (Exception ex) {
            throw new Error(ex);
        }

    }

    public static void creeEmploye(Connection con, String nom, String prenom, double salaire) throws SQLException {
        PreparedStatement p = con.prepareStatement("INSERT INTO EMPLOYE (nom, prenom, salaire) VALUES (?,?,?)");
        p.setString(1, nom);
        p.setString(2, prenom);
        p.setDouble(3, salaire);
        p.executeUpdate();
    }

    public static ResultSet nomsEmployes(Connection con) throws SQLException {
        PreparedStatement pst = con.prepareStatement(
                "SELECT DISTINCT NOM FROM EMPLOYE");
        return pst.executeQuery();
    }

    public static void main(String[] args) {
        try {
            System.out.println("coucou");
            creeEmploye(TestSGBD.defautCon, "olivier", "fresneda", 1);
        } catch (SQLException ex) {
            System.out.println("problème" + ex);
        } finally {
            if (TestSGBD.defautCon != null) {
                try {
                    TestSGBD.defautCon.close();
                } catch (SQLException ex) {
                    System.out.println("problème" + ex);
                }
            }
        }
    }
}
